package com.example.anew.journey.mynewjourney.logic.addDevices;

import android.text.SpannableString;
import android.text.Spanned;
import android.text.SpannedString;
import android.text.style.AbsoluteSizeSpan;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.example.anew.journey.mynewjourney.R;
import com.example.anew.journey.mynewjourney.base.BaseRegisteredAndLoginActivity;
import com.example.anew.journey.mynewjourney.logic.addDevices.persenter.AddDeviecsPersenter;
import com.example.anew.journey.mynewjourney.logic.addDevices.view.ISAddDeviecsView;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 杭州惟君医疗科技有限公司14
 * Created by 20433 on 2018/3/8
 */

public class AddDeviecsActivity extends BaseRegisteredAndLoginActivity implements ISAddDeviecsView,View.OnClickListener{
    @Bind(R.id.activity_add_devices_tv_devcies_name)
    TextView activityAddDevicesTvDevciesName;
    @Bind(R.id.activity_add_devices_tv_heart_rate_scope_show)
    TextView activityAddDevicesTvHeartRateScopeShow;
    @Bind(R.id.activity_add_devices_tv_heart_rate_scope_normal)
    TextView activityAddDevicesTvHeartRateScopeNormal;
    @Bind(R.id.activity_add_devices_tv_devices_picture)
    ImageView activityAddDevicesTvDevicesPicture;
    @Bind(R.id.activity_add_devices_bt_next)
    Button activityAddDevicesBtNext;

    private AddDeviecsPersenter mAddDeviecsPersenter = new AddDeviecsPersenter(this);
    private PopupWindow mPopWindow;
    private TextView tv_title_decs;
    private EditText et_input_banding_code;
    private EditText et_input_banding_code1;
    private TextView tv_banding_prompt;
    private TextView tv_input_banding_code_cancel;
    private TextView tv_banding_code_confirm;
    private PopupWindow mPopWindowBindingFailure;
    private TextView tv_desc;
    private TextView tv_error_content;
    private TextView tv_prompt;
    private TextView tv_end_binding;
    private TextView tv_rebind;
    private PopupWindow mPopWindowRemoveBinding;
    private TextView tv_title;
    private TextView tv_prompt_desc;
    private TextView tv_i_think;
    private TextView tv_confirm_unbundling;

    @Override
    protected void initView(View view) {
        // todo Toolbar的一系列操作
        setToolbarBg(0xffffffff);
        setReturnPicture(R.mipmap.cross);
        displayIvShared();
        setIvShared(R.mipmap.question_mark);
        activityAddDevicesBtNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                btNext();
            }
        });
    }


    @Override
    protected View setMainContentView() {
        View view = View.inflate(this, R.layout.activity_add_devices, null);
        ButterKnife.bind(this,view);
        return view;
    }
    /**
     * @desc 点击分享按钮
     */
    protected void onClickShared() {

    }
    private void btNext() {
        mAddDeviecsPersenter.onClickBtNext();
    }

    @Override
    protected void releaseResources() {
        ButterKnife.unbind(this);
    }
    // 获取下一步的文本内容
    @Override
    public String getBtNextContent() {
        return activityAddDevicesBtNext.getText().toString().trim();
    }
    // 设置下一步的文本内容
    @Override
    public void setBtNextContent(String str) {
        activityAddDevicesBtNext.setText(str);
    }
    // 显示提示Toast信息
    @Override
    public void showToastInformation(String str) {
        Toast.makeText(this,str,Toast.LENGTH_SHORT).show();
    }
    // 跳转页面
    @Override
    public void toOhterActivity(String niname) {

    }
    // 显示进度
    @Override
    public void displayProgress() {

    }
    // 隐藏进度
    @Override
    public void hiddenProgress() {

    }
    // TODO 输入绑定码
    //输入绑定码
    @Override
    public void showInputBandingCode() {
        //设置contentView
        View contentView = LayoutInflater.from(this).inflate(R.layout.input_banding_code, null);
        mPopWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
        mPopWindow.setContentView(contentView);
        findInputBandingCodeView(contentView);
        //显示PopupWindow
        View rootview = LayoutInflater.from(this).inflate(R.layout.activity_add_devices, null);
        mPopWindow.showAtLocation(rootview, Gravity.CENTER, 0, 0);
    }

    // 获取输入的绑定码
    @Override
    public String getInputBandingCode() {
        return null;
    }
    //输入绑定的码 取消
    @Override
    public void cancelInputBandingCode() {
        mPopWindow.dismiss();
    }
    //输入绑定的  确认绑定
    @Override
    public void inputBandingCodeConfirmBinding() {
        mPopWindow.dismiss();
    }
    // TODO 绑定失败
    //绑定失败
    @Override
    public void showBindingFailure() {
//设置contentView
        View contentView = LayoutInflater.from(this).inflate(R.layout.banding_failure, null);
        mPopWindowBindingFailure = new PopupWindow(contentView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
        mPopWindowBindingFailure.setContentView(contentView);
        findBindingFailure(contentView);
        //显示PopupWindow
        View rootview = LayoutInflater.from(this).inflate(R.layout.activity_add_devices, null);
        mPopWindowBindingFailure.showAtLocation(rootview, Gravity.CENTER, 0, 0);


    }
    //绑定失败 结束绑定
    @Override
    public void bindingFailureEndBinding() {
        mPopWindowBindingFailure.dismiss();
    }
    //绑定失败 重新绑定
    @Override
    public void bindingFailureRebind() {
        mPopWindowBindingFailure.dismiss();
    }
    // TODO 确认绑定失败
    //确认绑定失败
    @Override
    public void showRemoveBinding() {
        View contentView = LayoutInflater.from(this).inflate(R.layout.remove_banding, null);
        mPopWindowRemoveBinding = new PopupWindow(contentView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
        mPopWindowRemoveBinding.setContentView(contentView);
        findRemoveBinding(contentView);
        //显示PopupWindow
        View rootview = LayoutInflater.from(this).inflate(R.layout.activity_add_devices, null);
        mPopWindowRemoveBinding.showAtLocation(rootview, Gravity.CENTER, 0, 0);
    }


    //认绑定失败 我在想想
    @Override
    public void removeBindingIThink() {
        mPopWindowRemoveBinding.dismiss();
    }
    //认绑定失败 确认解绑
    @Override
    public void removeBindingConfirmUnbundling() {
        mPopWindowRemoveBinding.dismiss();
    }


    // 查找输入验证码的控件
    private void findInputBandingCodeView(View contentView) {
        tv_title_decs = (TextView)contentView.findViewById(R.id.input_banding_code_tv_title_decs);
        et_input_banding_code1 = (EditText)contentView.findViewById(R.id.input_banding_code_et_input_banding_code);
        tv_input_banding_code_cancel = (TextView)contentView.findViewById(R.id.input_banding_code_tv_input_banding_code_cancel);
        tv_banding_code_confirm = (TextView)contentView.findViewById(R.id.input_banding_code_input_tv_banding_code_confirm);

        SpannableString ss = new SpannableString("请在胎心仪背部标签查看绑定码");//定义hint的值
        AbsoluteSizeSpan ass = new AbsoluteSizeSpan(14,true);//设置字体大小 true表示单位是sp
        ss.setSpan(ass, 0, ss.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        et_input_banding_code1.setHint(new SpannedString(ss));

        tv_input_banding_code_cancel.setOnClickListener(this);
        tv_banding_code_confirm.setOnClickListener(this);
    }

    //查找绑定失败的控件
    private void findBindingFailure(View contentView) {
        tv_desc = (TextView)contentView.findViewById(R.id.banding_failure_tv_desc);
        tv_error_content = (TextView)contentView.findViewById(R.id.banding_failure_tv_error_content);
        tv_prompt = (TextView)contentView.findViewById(R.id.banding_failure_tv_prompt);
        tv_end_binding = (TextView)contentView.findViewById(R.id.banding_failure_tv_end_binding);
        tv_rebind = (TextView)contentView.findViewById(R.id.banding_failure_tv_rebind);
        tv_end_binding.setOnClickListener(this);
        tv_rebind.setOnClickListener(this);
    }
    //查找确认绑定失败的控件
    private void findRemoveBinding(View contentView) {
        tv_title = (TextView)contentView.findViewById(R.id.remove_banding_tv_title);
        tv_prompt_desc = (TextView)contentView.findViewById(R.id.remove_banding_device_tv_prompt_desc);
        tv_i_think = (TextView)contentView.findViewById(R.id.remove_banding_tv_i_think);
        tv_confirm_unbundling = (TextView)contentView.findViewById(R.id.remove_banding_tv_confirm_unbundling);

        tv_i_think.setOnClickListener(this);
        tv_confirm_unbundling.setOnClickListener(this);
    }
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.input_banding_code_tv_input_banding_code_cancel: //输入绑定的码 取消
                mAddDeviecsPersenter.cancelInputBandingCode();
                break;
            case R.id.input_banding_code_input_tv_banding_code_confirm:  //输入绑定的  确认绑定
                mAddDeviecsPersenter.inputBandingCodeConfirmBinding();
                break;
            case R.id.banding_failure_tv_end_binding:    //绑定失败 结束绑定
                mAddDeviecsPersenter.bindingFailureEndBinding();
                break;
            case R.id.banding_failure_tv_rebind:   //绑定失败 重新绑定
                mAddDeviecsPersenter.bindingFailureRebind();
                break;
            case R.id.remove_banding_tv_i_think:    //认绑定失败 我在想想
                mAddDeviecsPersenter.verifyBindingFailedIThink();
                break;
            case R.id.remove_banding_tv_confirm_unbundling:  //认绑定失败 确认解绑
                mAddDeviecsPersenter.verifyBindingFailedConfirmUnbundling();
                break;
        }
    }
}
